********************************************************************************
** 	TITLE: e19_kites_eventstudy_courtclosure.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XIX: Kites Court Closure Event Study

********************************************************************************
	set sortseed 13
	
		use "$output_data/balanced_panel_temp.dta", clear
	
		* merge kites - defendants as senders
		preserve
		use "$input_data/kites_11jan2023/kites_cleaned_messages_identified.dta", clear
		keep request_number inmate datetime* response* sender* remove1
	
		* identifying episodes faced scheduling before court closures
		g pre_anyresch = (remove1 <= td(17mar2020))
		g ever_resch = (!mi(remove1))
		
		reshape long datetime response sender, i(request_number pre_anyresch) j(n_mess)
		rename datetime date
		drop if date == .
		drop if sender == 1
		
		* concatened messages in the same date
		sort inmate date, stable
		by inmate date: gen alltext = response[1]
		by inmate date: replace alltext = alltext[_n-1] + " " + response if _n > 1
		by inmate date: replace alltext = alltext[_N]
		by inmate date: keep if _n == _N
		drop response
		rename alltext response		
		
		tempfile formerge
		save `formerge'
		restore
		
		* merge kites - deputy as senders
		preserve
		use "$input_data/kites_11jan2023/kites_cleaned_messages_identified.dta", clear
		keep request_number inmate datetime* response* sender* remove1
	
		* identifying episodes faced scheduling before court closures
		g pre_anyresch = (remove1 <= td(17mar2020))
		g ever_resch = (!mi(remove1))
		
		reshape long datetime response sender, i(request_number pre_anyresch) j(n_mess)
		rename datetime date
		drop if date == .
		drop if sender == 0
		
		* concatened messages in the same date
		sort inmate date, stable
		by inmate date: gen alltext = response[1]
		by inmate date: replace alltext = alltext[_n-1] + " " + response if _n > 1
		by inmate date: replace alltext = alltext[_N]
		by inmate date: keep if _n == _N
		drop response
		rename alltext response		
		
		tempfile formerge_deputy
		save `formerge_deputy'
		restore
	
		
		merge m:1 inmate date using `formerge', keep(1 3) nogen
		
		*--------------------------------*
			* Here we start to prepare data for the DiD
		*--------------------------------*
		drop request_number n_mess sender
		
		** create weekly and monthly data
		gen int week_num = wofd(date) - wofd(td(02mar2019))
												
		* constructing ouctomes suggested
		* court date
		gen D_court_date = strpos(response,"court")>0 & ///
							(strpos(response,"when")>0| ///
							strpos(response,"date")>0| ///
							strpos(response,"time")>0| ///
							strpos(response,"day")>0)

		gen D_talk = strpos(response,"talk")>0
		gen D_speak = strpos(response,"speak")>0
		gen D_need = strpos(response,"need")>0
		gen D_can = strpos(response,"can")>0				
		gen D_begging = strpos(response,"talk")>0 | ///
							strpos(response,"speak")>0 | ///
							strpos(response,"need")>0 | ///
							strpos(response,"can")>0
							
		gen D_talk_speak = strpos(response,"talk")>0 | ///
							strpos(response,"speak")>0

		
		gen D_thank_please = strpos(response,"thank")>0 | ///
							strpos(response,"thanks")>0 | ///
							strpos(response,"please")>0
							
		* combination of thanks
		gen D_thank_please_talk_speak = (D_talk_speak == 1 | D_thank_please == 1)
		
		* Combined Asks
		gen D_combined = strpos(response,"talk")>0 | ///
							strpos(response,"speak")>0 | ///
							strpos(response,"please")>0 | ///
							strpos(response,"can")>0 | ///
							strpos(response,"court")>0 | ///
							strpos(response,"need")>0 | ///
							strpos(response,"judge")>0
				
		* post -- week treament 
		gen int week_tre = wofd(td(17mar2020)) - wofd(td(02mar2019))		
		gen int week_tre_placebo = wofd(td(17may2019)) - wofd(td(02mar2019))		
	
		** define local for treatments
		global outcomes D_combined D_court_date D_talk D_speak D_need D_can D_begging D_talk_speak D_thank_please D_thank_please_talk_speak 
		
		** collapse data at the week level	
		collapse (sum) N_combined=D_combined N_court_date=D_court_date N_talk=D_talk N_speak=D_speak N_need=D_need N_can=D_can N_begging=D_begging N_talk_speak=D_talk_speak N_thank_please=D_thank_please ///
		N_thank_please_talk_speak=D_thank_please_talk_speak (max) $outcomes ever_resch pre_anyresch (firstnm) week_tre week_tre_placebo, by(inmate week_num)

		global outcomes D_combined
		
		label var D_combined "Court-related texts"
		
		* create interaction variables
		g post = (week_num >= week_tre)
		g t = week_num - week_tre
		
		* create interaction variables -- placebo
		g post_pla = (week_num >= week_tre_placebo)
		g t_pla = week_num - week_tre_placebo
		
				
		* gen post variable
		local w = 4
		
		** weeks for court closure
		forvalues day = -`w'(1)`w' {
			if `day' < -1 {
				local tname = abs(`day')
				g Dg_`tname' = (t == `day')
			}
			else if `day' >= 0 {
				g Dg`day' = (t == `day')
			}
		}
		
		** weeks for placebos
		forvalues day = -`w'(1)`w' {
			if `day' < -1 {
				local tname = abs(`day')
				g pDg_`tname' = (t_pla == `day')
			}
			else if `day' >= 0 {
				g pDg`day' = (t_pla == `day')
			}
		}
			
		ds Dg*
			local vars `r(varlist)'

		ds pDg*
			local pvars `r(varlist)'	

/*********************************************************************************
Panel (a) Court Closure
**********************************************************************************/				
			// balance panel
			foreach y in $outcomes {
				preserve
				local label : variable label `y' 
				
				** dynamic TE -- balanced sample
				keep if t >= -`w' & t <= `w'
				bys inmate: g id = 1
				bys inmate: egen total_id = total(id)
				
				reghdfe `y' `vars' if total_id == 9, a(inmate) vce(cl inmate)
				
				mat define R = J(8,3,.)
				local i=1
				forval j=4(-1)2{
					mat R[`i',1] = _b[Dg_`j']
					mat R[`i',2] = _se[Dg_`j']
					mat R[`i',3] = -`j'
					local i = `i'+1
				}
				forval j=0/4{
					mat R[`j'+4,1] = _b[Dg`j']
					mat R[`j'+4,2] = _se[Dg`j']
					mat R[`j'+4,3] = `j'
				}
				restore
				* figure	
				preserve	

					svmat R
					keep R1-R3
					drop if mi(R1)
	
					
					set obs `=_N+1'
					replace R3 = -1 if _n==_N
					replace R1 = 0 if _n==_N
					
					gen ub = R1+1.96*R2
					gen lb = R1-1.96*R2
					
					local min = -`w'-.5 
					local max =`w'+.5
					
					tw (scatter R1 R3, mc(black) msymbol(O)) ///
						(rcap ub lb R3, lc(black)) ///
						, ///
						xline(-.5, lp(solid) lc(gs10) lw(med)) ///
						yline(0,  lc(black)) ylab(-0.15(.1).25) ///
						xlab(-`w'(1)`w') xscale(range(`min' `max')) ///
						legend(off) xtitle("Week since court closure") ///
						ytitle("`label'")
						graph export "${figures}/ES_`y'_prepost_balanced.pdf", replace
				restore
			
			
		}

		
		

/*********************************************************************************
Panel (b) Placebo
**********************************************************************************/			
			// balanced panel -- placebo
			foreach y in $outcomes {
				preserve
				local label : variable label `y' 
		
				** dynamic TE -- balanced sample
				keep if t_pla >= -`w' & t_pla <= `w'
				bys inmate: g id = 1
				bys inmate: egen total_id = total(id)
				
				reghdfe `y' `pvars' if total_id == 9, a(inmate) vce(cl inmate)
				mat define R = J(8,3,.)
				local i=1
				forval j=4(-1)2{
					mat R[`i',1] = _b[pDg_`j']
					mat R[`i',2] = _se[pDg_`j']
					mat R[`i',3] = -`j'
					local i = `i'+1
				}
				forval j=0/4{
					mat R[`j'+4,1] = _b[pDg`j']
					mat R[`j'+4,2] = _se[pDg`j']
					mat R[`j'+4,3] = `j'
				}
				restore
				* figure	
				preserve	

					svmat R
					keep R1-R3
					drop if mi(R1)
	
					
					set obs `=_N+1'
					replace R3 = -1 if _n==_N
					replace R1 = 0 if _n==_N
					
					gen ub = R1+1.96*R2
					gen lb = R1-1.96*R2
					
					local min = -`w'-.5 
					local max =`w'+.5
					
					tw (scatter R1 R3, mc(black) msymbol(O)) ///
						(rcap ub lb R3, lc(black)) ///
						, ///
						xline(-.5, lp(solid) lc(gs10) lw(med)) ///
						yline(0,  lc(black)) ylab(-.15(.1).25) ///
						xlab(-`w'(1)`w') xscale(range(`min' `max')) ///
						legend(off) xtitle("Week since placebo date") ///
						ytitle("`label'")
						
						graph export "${figures}/ES_`y'_prepost_balanced_placebo.pdf", replace
				restore		
		}	